using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text.Json.Nodes;
using RndMaterialDisposalService.Services.Interface;


namespace RndMaterialDisposalService.Pages.EmailTemplates.LaunchVerify
{
    /// <summary>
    /// 
    /// </summary>
    [IgnoreAntiforgeryToken]
    public class IndexModel : PageModel
    {
        private readonly ISMOService service;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_service"></param>
        public IndexModel(ISMOService _service)
        {
            service = _service;
        }
        /// <summary>
        /// 
        /// </summary>
        public JsonObject FormData { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public void OnPost(string jsonData) {
            FormData = JsonNode.Parse(jsonData)?.AsObject() ?? new JsonObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formid"></param>
        /// <returns></returns>
        public async Task<IActionResult> OnGetAsync(int? formid)
        {
            if (formid == null)
            {
                return NotFound();
            }
            var data = await service.GetApplicationDataByFormId(formid ?? 0);
            if (data == null || string.IsNullOrEmpty(data.FormData))
            {
                return NotFound();
            }
            FormData = JsonNode.Parse(data.FormData)?.AsObject() ?? new JsonObject();
            return Page();
        }
    }
}
